package huzh.foundation.log;

import huzh.foundation.startup.AppEnv;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.PropertyConfigurator;

/**
 *<p>title:LogMgr</p>
 *<p>description:</p>
 * @since 2014-6-6
 * @author huzh
 * @version 1.0
 */
public class LogMgr {
    /**
     * @param logConfigFilePath
     * @return
     */
    public static synchronized boolean init(String logConfigFilePath){
        try{
            System.setProperty("org.apache.commons.logging.LogFactory", "org.apache.commons.logging.impl.Log4jFactory");
            System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.Log4JCategoryLog");
            PropertyConfigurator.configure(logConfigFilePath);
            System.out.println("Success Init Log !!!");
            return true;
        } catch (Exception ex) {
            System.out.println("Failed Init Log !!!");
            System.out.println(ex.getMessage());
            ex.printStackTrace();
        }
        return false;
    }

    /**
     * @param c
     * @return
     */
    public static Log getLog(Class<?> c){
        return LogFactory.getLog(c);
    }

    /**
     * @param c
     * @param s
     */
    public static void debug(Class<?> c, String s) {
        if (AppEnv.isDebug()) {
            System.out.println(s);
        }
        getLog(c).debug(s);
    }

    /**
     * @param c
     * @param s
     */
    public static void error(Class<?> c, String s) {
        if (AppEnv.isDebug()) {
            System.out.println(s);
        }
         getLog(c).error(s);
    }

    /**
     * @param c
     * @param s
     * @param e
     */
    public static void error(Class<?> c, String s, Throwable e) {
        if (AppEnv.isDebug()) {
            System.out.println(s);
        }
        getLog(c).error(s, e);
    }

    /**
     * @param c
     * @param s
     */
    public static void fatal(Class<?> c, String s) {
        if (AppEnv.isDebug()) {
            System.out.println(s);
        }
        getLog(c).fatal(s);
    }

    /**
     * @param c
     * @param s
     */
    public static void info(Class<?> c, String s) {
        if (AppEnv.isDebug()) {
            System.out.println(s);
        }
        getLog(c).info(s);
    }

    /**
     * @param c
     * @param s
     */
    public static void warn(Class<?> c, String s) {
        if (AppEnv.isDebug()) {
            System.out.println(s);
        }
        getLog(c).warn(s);
    }
}
